﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace PingPong
{
    class Pallete : Object 
    {
        public int Pontos = 0;

        public Pallete(GameWindow janela, Texture2D texture, Vector2 position)
            : base(janela, texture, position) 
        {
            this.position = position;
            this.velocity = new Vector2(0,5);
            this.size = texture.Bounds;   
        }

        public override void Update(){}

        public void Update(GameWindow janela, GameTime gametime, List<Keys> teclas) 
        {
            ManterNaTela(janela);

            foreach (Keys tecla in teclas)
            {
                if (Keyboard.GetState().IsKeyDown(tecla) && tecla == Keys.Up) 
                {
                    position -= velocity; 
                }

                if(Keyboard.GetState().IsKeyDown(tecla) && tecla == Keys.Down)
                {
                    position += velocity; 
                }

                if (Keyboard.GetState().IsKeyDown(tecla) && tecla == Keys.W) 
                {
                    position -= velocity; 
                }

                if (Keyboard.GetState().IsKeyDown(tecla) && tecla == Keys.S) 
                {
                    position += velocity; 
                }   
            }

            // atualiza a posicao do rect da bola de acordo com a sua posicao
            this.size.X = (int) position.X;
            this.size.Y = (int)position.Y;  
        }

        // verifica se a pallete saiu do extremos da janela
        private void ManterNaTela(GameWindow janela)
        {
            if (position.Y <= 0)
            {
                position.Y = 0; 
            }

            if (position.X <= 0) 
            {
                position.X = 0; 
            }

            if (position.Y >= janela.ClientBounds.Height - size.Height) 
            {
                position.Y = janela.ClientBounds.Height - size.Height;     
            }

            if (position.X >= janela.ClientBounds.Width - size.Width) 
            {
                position.X = janela.ClientBounds.Width - size.Width;     
            } 
        }

        //Desenha a pallete na tela
        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, position, Color.White);      
        }
         
        public BoundingBox GetBounding() 
        {
            return new BoundingBox(new Vector3(this.position.X, this.position.Y, 0.0f),
                new Vector3(this.position.X + this.size.Width, this.position.Y + this.size.Height, 0.0f));                      
        }

        public void palleteIA(GameTime gametime, GameWindow janela, Bola bola )
        {
            if (bola.Position.X > 500.0f)
            {
                if (this.Position.Y + this.size.Height / 2 + 10.0f < bola.Position.Y + bola.Size.Height / 2 ||
                   this.Position.Y + this.size.Height / 2 - 10.0f < bola.Position.Y + bola.Size.Height / 2)
                {
                    this.position.Y += this.velocity.Y;
                }
                else
                {
                    this.position.Y -= this.velocity.Y;
                }
            }

            if (bola.Position.X < 300.0f)
            {
                if (this.position.Y < 290)
                {
                    this.position.Y += this.velocity.Y;
                }
                else
                {
                    this.position.Y -= this.velocity.Y;
                }
            }

        }

    }
}
